package com.niodata.dp.plugin.core;

import com.niodata.dp.util.JsonUtil;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

/**
 * plugin service for local debugging.
 */
public class PluginServiceLocal implements PluginService {

  private PluginContext pluginContext;

  public PluginServiceLocal(PluginContext context) {
    this.pluginContext = context;
  }

  @Override
  public void saveState(PluginState state) throws IOException {

  }

  @Override
  public PluginState getState(String taskName, String scheduleId, String jobName)
        throws IOException {
    return null;
  }

  @Override
  public void outputValues(PluginOutputValues outpuValues) throws IOException {
    File file = new File("outputValues.txt");
    BufferedWriter writer = null;
    try {
      writer = new BufferedWriter(new FileWriter(file, false));
      Map<String, Object> values = outpuValues.getValues();
      for (String key : values.keySet()) {
        writer.write(key + "=" + JsonUtil.toJson(values.get(key)));
        writer.newLine();
      }
    } finally {
      if (writer != null) {
        writer.close();
      }
    }

  }

  @Override
  public void output(Output output) throws IOException {
    File file = new File("outputValues.txt");
    BufferedWriter writer = null;
    try {
      writer = new BufferedWriter(new FileWriter(file, false));
      String json = JsonUtil.toJson(output);
      writer.write(json);
    } finally {
      if (writer != null) {
        writer.close();
      }
    }
  }

  @Override
  public void publishEvent(Map<String, Object> message) throws IOException {

  }

  @Override
  public void saveTableInfo(String taskName, String dsName, String dbName, String tableName,
        String tableDesc) throws IOException {

  }

  @Override
  public void saveTableInfo(String dsName, String dbName, String tableName, String tableDesc)
        throws IOException {

  }

  public PluginContext getPluginContext() {
    return pluginContext;
  }

  public void setPluginContext(PluginContext pluginContext) {
    this.pluginContext = pluginContext;
  }
}
